package com.bluefish.bltuan.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.bluefish.bltuan.dao.UserDao;
import com.bluefish.bltuan.module.user.AbstractUser;
import com.bluefish.bltuan.module.user.User;

@Repository
public class UserDaoImpl extends AbstractDaoImpl<User, Integer> implements UserDao {

    /**
     * 
     */
    private static final long   serialVersionUID = 7237234254569244718L;

    private static final String USERNAME         = "username";

    private static final String PASSWOED         = "password";

    private static final String EMAIL            = "email";

    private static final String ACTIVE_CODE      = "activeCode";

    public AbstractUser findByUserId(Integer id) {
        return this.getHibernateTemplate().get(AbstractUser.class, id);
    }

    public User findByEmail(String email) {
        List<User> users = listByProperty(EMAIL, email);
        return users.isEmpty() ? null : users.get(0);
    }

    public User findByUsername(String username) {
        List<User> users = listByProperty(USERNAME, username);
        return users.isEmpty() ? null : users.size() > 1 ? null : users.get(0);
    }

    public User getByEmailAndPasswordl(String email, String password) {
        List<User> users = listByPropertys(new String[] { EMAIL, PASSWOED }, new String[] { email,
                password });
        return users.isEmpty() ? null : users.get(0);
    }

    public User findByEmailAndActiveCode(String email, String activeCode) {
        List<User> users = listByPropertys(new String[] { EMAIL, ACTIVE_CODE }, new Object[] {
                email, activeCode });
        return users.isEmpty() ? null : users.size() > 1 ? null : users.get(0);
    }
}
